<template>
  <div>
    <!-- 功能 -->
    <el-row :gutter="20" class="row_box">
      <el-col :span="2"
        ><div class="col_box" @click="dialogFormVisible = true">
          新建
        </div></el-col
      >
      <el-col :span="8"><div class="col_box">按关键字搜索</div></el-col>
      <el-col :span="2" :push="12"><div class="col_box">撤销</div></el-col>
    </el-row>

    <!-- 弹出层 -->
    <el-dialog
      title="新增/编辑用户"
      :visible.sync="dialogFormVisible"
      width="30%"
      center
      modal
      :show-close="false"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input v-model="name" placeholder="请输入名称"></el-input
        ></el-col>
        <el-col :span="12">
          <el-select v-model="selectValue" placeholder="性别">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option> </el-select
        ></el-col>
      </el-row>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-input
            v-model="dialog_phone"
            placeholder="请输入联系方式"
          ></el-input
        ></el-col>
        <el-col :span="12">
          <el-input v-model="dialog_age" placeholder="年龄"></el-input
        ></el-col>
      </el-row>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="11">
          <el-cascader
            v-model="cascaderValue"
            :options="cascaderOptions"
            :props="{ expandTrigger: 'hover' }"
            size="mini"
          ></el-cascader>
        </el-col>
        <el-col :span="13">
          <el-input size="mini" v-model="dizi" placeholder="详细地址"></el-input
        ></el-col>
      </el-row>
      <el-row
        :gutter="20"
        justify="center"
        type="flex"
        style="margin-top: 10px"
      >
        <el-col :span="5">
          <el-button type="info" @click="dialogFormVisible = false"
            >取消</el-button
          ></el-col
        >
        <el-col :span="5"
          ><el-button type="primary" @click="save">{{
            delSet
          }}</el-button></el-col
        >
      </el-row>
    </el-dialog>

    <!-- 展示表格 -->
    <el-table :data="tableData" @select="selectionRow" style="width: 100%">
      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="80" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="60" align="center">
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="60" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="联系电话" width="180" align="center">
      </el-table-column>
      <el-table-column prop="address" label="地址" width="300" align="center">
      </el-table-column>
      <el-table-column prop="sex" label="编辑" align="center" width="200">
        <template slot-scope="scope">
          <div class="column_btn_box">
            <div class="column_btn" @click="set_btn(scope.row, scope.$index)">
              编辑
            </div>
            <div class="column_btn" @click="del(scope.$index)">删除</div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="del_box">批量删除</div>
  </div>
</template>

<script>
export default {
  name: "fun",
  data() {
    return {
      delSet: "保存",
      name: "",
      selectValue: "",
      cascaderValue: "",
      dizi: "",
      dialog_phone: "",
      dialog_age: "",
      dialogFormVisible: false,
      csid: 1,
      tableData: JSON.parse(localStorage.getItem("todoList")) || [
        {
          name: "王小虎",
          age: "男",
          sex: 10,
          phone: "176076",
          address: "阿士大夫和sad",
          check: false,
          id: 1,
        },
        {
          name: "王小虎",
          age: "男",
          sex: 10,
          phone: "176076",
          address: "阿士大夫和sad",
          check: false,
          id: 2,
        },
      ],
      options: [
        {
          value: "男",
          label: "男",
        },
        {
          value: "女",
          label: "女",
        },
      ],
      cascaderOptions: [
        {
          value: "广东省",
          label: "广东省",
          children: [
            {
              value: "广州市",
              label: "广州市",
              children: [
                {
                  value: "天河区",
                  label: "天河区",
                },
                {
                  value: "白云区",
                  label: "白云区",
                },
                {
                  value: "越秀区",
                  label: "越秀区",
                },
                {
                  value: "海珠区",
                  label: "海珠区",
                },
              ],
            },
          ],
        },
      ],
    };
  },
  watch: {
    tableData: {
      deep: true,
      handler() {
        // 8.0 只要tableData变化 - 覆盖式保存到localStorage里
        localStorage.setItem("todoList", JSON.stringify(this.tableData));
      },
    },
  },
  methods: {
    // 判断选中
    selectionRow(selection, row) {
      row.check == true ? (row.check = false) : (row.check = true);
    },
    // 新增数据
    save() {
      if (this.name == "" && this.dialog_age == "" && this.selectValue == "")
        return this.$message({
          message: "请全部输入框都输入内容哦",
          type: "warning",
        });
      if (this.delSet == "保存") {
        let newid = this.tableData.length + 1;
        this.tableData.push({
          name: this.name,
          age: this.dialog_age,
          sex: this.selectValue,
          phone: this.dialog_phone,
          address: this.dizi,
          check: false,
          id: this.tableData.length == 0 ? newid : this.tableData.length + 1,
        });
      } else {
        this.tableData[this.csid].name = this.name;
        this.tableData[this.csid].dialog_age = this.dialog_age;
        this.tableData[this.csid].selectValue = this.selectValue;
        this.tableData[this.csid].dialog_phone = this.dialog_phone;
        this.tableData[this.csid].dizi = this.dizi;
      }
      this.dialogFormVisible = false;
    },
    del(index) {
      this.tableData.splice(index, 1);
    },
    adddel() {},
    set_btn(e, index) {
      this.csid = index;
      this.name = e.name;
      this.dialog_age = e.age;
      this.selectValue = e.sex;
      this.dialog_phone = e.phone;
      this.dizi = e.address;
      this.delSet = "修改";
      this.dialogFormVisible = true;
    },
  },
};
</script>

<style>
.row_box {
  margin-bottom: 10px;
}
.col_box {
  height: 40px;
  background-color: #f5f5f5;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: 800;
  border-radius: 3px;
}
.column_btn_box {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.column_btn {
  background-color: #f5f5f5;
}
.del_box {
  background-color: #f5f5f5;
  margin-top: 10px;
  width: 80px;
  height: 30px;
  display: flex;
  justify-content: center;
  font-weight: 800;
  align-items: center;
}

.dialog_box_1 {
  display: flex;
}
</style>
